<?php
class qModuleInfo
{
    /** Name of module. */
    private $name;
    /** Module info of parent. */
    private $parent;
    /** Module object. */
    private $module;
    /** Array of submodules. */
    private $submodules = array();
    
    /**
     * Constructor.
     * @param string $name name of module
     * @param module $module module object
     * @param string $parent parent of module
     */
    function __construct($name, $module, $parent = null)
    {
        $this->name = $name;
        $this->parent = $parent;
        $this->module = $module;
    }
    
    /**
     * Adds submodule.
     * @param string $name name of module
     * @param module $module module object
     */
    public function AddSubmodule($name, $module)
    {
        $submodule = new qModuleInfo($name, $module, $this);
        $this->submodules[] = $submodule;
    }
    
    /**
     * Returns module name.
     * @return Name of module.
     */
    public function Name()
    {
        return $this->name;
    }
    
    /**
     * Returns module object.
     * @return Module object.
     */
    public function Module()
    {
        return $this->module;
    }
    
    /**
     * Returns parent's module info.
     * @return Module info of parent.
     */
    public function Parent()
    {
        return $this->parent;
    }
    
    /**
     * Returns "pathstring" of parents (ex. parentofparent/parent/module).
     * @return "Pathstring" of parents.
     */
    public function Parents()
    {
        if ($this->parent != null)
        {
            return $this->parent->Parents() . '/' . $this->Parent();
        }
        else
        {
            return '';
        }
    }
    
    /**
     * Returns submodule named $name ($name can be a "pathname".
     * @param type $name name or "pathname" of module
     * @return Found moduleinfo / false.
     */
    public function GetModule($name)
    {
        foreach ($this->submodules as $module)
        {
            // found...
            if ($module->name == $name)
            {
                return $module;
            }
            list($parent, $name) = explode('/', $name, 1);
            // name is not a pathname
            if (!$parent || !$name) return false;
            
            // name is a pathname, ask it's parent
            if ($parent == $module->name)
            {
                $module->GetModule($name);
            }
        }
        
        return false;
    }
}